﻿
using Microsoft.EntityFrameworkCore;
using System.Linq;
using System.Threading.Tasks;
using LPP.EntityFrameworkCore;
using LPP.Entity;

namespace LPP.Repository
{
    public class SmsVerificationRepository
    {
        private readonly SqlDbContext _dbContext;

        public SmsVerificationRepository(SqlDbContext dbContext)
        {
            _dbContext = dbContext;
        }

        public async Task AddAsync(SmsVerification entity)
        {
            await _dbContext.SmsVerifications.AddAsync(entity);
            await _dbContext.SaveChangesAsync();
        }

        public async Task UpdateAsync(SmsVerification entity)
        {
            _dbContext.Entry(entity).State = EntityState.Modified;
            await _dbContext.SaveChangesAsync();
        }

        public async Task<SmsVerification> GetLatestByPhoneNumberAsync(string phoneNumber)
        {
            return await _dbContext.SmsVerifications
                .Where(x => x.PhoneNumber == phoneNumber)
                .OrderByDescending(x => x.CreatedAt)
                .FirstOrDefaultAsync();
        }
    }
}
